package com.zsrd.auth.config;

//import cn.dev33.satoken.interceptor.SaAnnotationInterceptor;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
//import com.laker.admin.framework.ext.interceptor.TraceAnnotationInterceptor;
//import com.laker.admin.framework.ext.mvc.CurrentUser;
//import com.laker.admin.framework.ext.mvc.PageRequestArgumentResolver;
//import com.laker.admin.framework.ext.mvc.StringToEnumConvertFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsrd.system.domain.SysPower;
import com.zsrd.system.service.SysPowerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.Wrapper;
import java.util.List;

/**
 * @author sjy
 */
@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {


    private static final String[] exclude_path = {
            "/error",
            "/swagger-resources/**",
            "/auth/login"};


    @Autowired
    private SysPowerService sysPowerService;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 配置跨域访问
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("*")
                .maxAge(3600);
    }

    /**
     * 注册sa-token的拦截器，打开注解式鉴权功能 (如果您不需要此功能，可以删除此类)
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        try {
            LambdaQueryWrapper<SysPower> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(SysPower::getEnable,1);
            List<SysPower> powers = sysPowerService.list(queryWrapper);
            SaInterceptor saInterceptor=new SaInterceptor(h->{
                StpUtil.checkLogin();
                if(powers.size()>0){
                    powers.forEach(p->{
                        // 根据路由划分模块，不同模块不同鉴权
                        SaRouter.match(p.getPowerUrl(), r -> StpUtil.checkPermissionOr(p.getPowerCode(),"power:admin"));//如果是超管角色可跳过所有权限认证
                    });
                }
            });
            // 注册注解拦截器，并排除不需要注解鉴权的接口地址 (与登录拦截器无关)
            registry.addInterceptor(saInterceptor).addPathPatterns("/**")
                    .excludePathPatterns(exclude_path);
        }catch (NotLoginException e){
            throw e;
        }

//        // 配置自定义拦截器
//        registry.addInterceptor(new TraceAnnotationInterceptor()).addPathPatterns("/**")
//                .excludePathPatterns(trace_exclude_path);
    }
//
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        // 配置静态资源，自定义虚拟磁盘功能
//        File web = new File("web");
//        String path = lakerConfig.getOssFile().getPath();
//        File file = new File(path);
//        if (!file.exists()) {
//            file.mkdirs();
//        }
//        log.info(file.getAbsolutePath());
//        registry.addResourceHandler("/admin/**")
//                .addResourceLocations("file:" + web.getAbsolutePath() + "/admin/");
//
//        registry.addResourceHandler("/" + path + "/**")
//                .addResourceLocations("file:" + file.getAbsolutePath() + "/");
//    }
//
//
//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//        // 路由匹配规则
////        configurer.setUseTrailingSlashMatch(false);
//    }
//
//
//    @Override
//    public void addFormatters(FormatterRegistry registry) {
//        // 添加自定义类型转换器
//        // 比如将字符串转换为日期类型，可通过DateFormatter类来实现自动转换。
//        // formatters和converters用于对日期格式进行转换，默认已注册了Number和Date类型的formatters，
//        // 支持@NumberFormat和@DateTimeFormat注解，需要自定义formatters和converters可以实现addFormatters方法。
////        registry.addFormatter(new DateFormatter("yyyy-MM-dd"));
//        registry.addConverter(new StringToEnumConvertFactory());
//    }
//
//    @Override
//    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
//        // 配置异常解析器
//    }
//
//
//    @Override
//    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
//        // 添加自定义方法参数处理器
//        argumentResolvers.add(new PageRequestArgumentResolver());
//        argumentResolvers.add(new HandlerMethodArgumentResolver() {
//            @Override
//            public boolean supportsParameter(MethodParameter parameter) {
//                return CurrentUser.class.equals(parameter.getParameterType());
//            }
//
//            @Override
//            public Object resolveArgument(
//                    MethodParameter parameter,
//                    ModelAndViewContainer mvContainer,
//                    NativeWebRequest nativeWebRequest,
//                    WebDataBinderFactory webDataBinderFactory) throws Exception {
//                HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
//                if (request == null) {
//                    return null;
//                }
//                // CurrentUser  = from request
//                return CurrentUser.builder().id(StpUtil.getLoginId().toString()).build();
//            }
//        });
//    }
}
